package com.byt.framlib.commonutils;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.Build;
import android.view.View;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.byt.framlib.R;
import com.byt.framlib.base.BaseActivity;
import com.byt.framlib.commonwidget.LoadingTip;

public class MyWebViewClient extends WebViewClient {

    private LoadingTip mLoadingTip;
    private WebView mWebView;
    private BaseActivity mContext;

    public MyWebViewClient(LoadingTip loadingTip, WebView webView, BaseActivity context) {
        mLoadingTip = loadingTip;
        mWebView = webView;
        mContext = context;
    }

    //在开始加载网页时会回调
    @Override
    public void onPageStarted(WebView webView, String s, Bitmap bitmap) {
        super.onPageStarted(webView, s, bitmap);
        mLoadingTip.setLoadingTip(LoadingTip.LoadStatus.finish);
        mWebView.setVisibility(View.VISIBLE);
    }

    //加载错误的时候会回调
    @Override
    public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
        super.onReceivedError(webView, errorCode, description, failingUrl);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            return;
        }
        mLoadingTip.setLoadingTip(LoadingTip.LoadStatus.error);
        mLoadingTip.setRealoadVisibility(false);
        mLoadingTip.setTips("抱歉,您访问的页面不存在~" + errorCode + "  " + description );
        mLoadingTip.setTipLogo(R.drawable.ic_web_no_data, (int) mContext.getResources().getDimension(R.dimen.x600),
                (int) mContext.getResources().getDimension(R.dimen.y600));
        mWebView.setVisibility(View.INVISIBLE);
    }

    //加载错误的时候会回调
    @SuppressLint("NewApi")
    @Override
    public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
        super.onReceivedError(webView, webResourceRequest, webResourceError);
        if (webResourceRequest.isForMainFrame()) {
            mLoadingTip.setLoadingTip(LoadingTip.LoadStatus.error);
            mLoadingTip.setRealoadVisibility(false);
            mLoadingTip.setTips("抱歉,您访问的页面不存在~\n"  + webResourceError.getErrorCode() + "\n错误信息:" + webResourceError.getDescription().toString());
            mLoadingTip.setTipLogo(R.drawable.ic_web_no_data, (int) mContext.getResources().getDimension(R.dimen.x600),
                    (int) mContext.getResources().getDimension(R.dimen.y600));
            mWebView.setVisibility(View.INVISIBLE);
        }
    }

    //加载完成的时候会回调
    @Override
    public void onPageFinished(WebView webView, String s) {

    }

    @SuppressLint("MissingPermission")
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("http:") || url.startsWith("https:")) {
            view.loadUrl(url);
            return false;
        }
        return false;
    }



}
